#include "XServeTask.h"
#include <event2/event.h>
#include <event2/listener.h>
#include <iostream>
using namespace std;

static void SLitenCB(struct evconnlistener *listener, evutil_socket_t sock, struct sockaddr *sa, int socklen, void *arg){
    cout<<"XServeTask SLitenCB"<<endl;
    XServeTask *task = (XServeTask*)arg;
    if(task->m_listenCB)
        task->m_listenCB(sock,sa,socklen,arg);
    else
        cerr<<"XServeTask::init:m_listenCB is null"<<endl;
}

bool XServeTask::Init()
{
    if (m_servePort <= 0)
    {
        cerr<<"XServeTask::init:port<=0"<<endl;
        return false;
    }
    sockaddr_in sin = {0};
    sin.sin_family = AF_INET;
    evutil_inet_pton(AF_INET, m_ip, &sin.sin_addr.s_addr);
    sin.sin_port = htons(m_servePort);
    evconnlistener_new_bind(
        base(),
        SLitenCB,
        this,
        LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
        10,//backlog:监听队列长度
        (sockaddr*)&sin,
        sizeof(sin)
        );
    return false;
}